package mesweb.mesweb.controller.procedure;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.farvision.common.api.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import mesweb.mesweb.entity.Procedure.MesProduceProcedureBranchProcedure;
import mesweb.mesweb.service.procedure.IMesProduceProcedureBranchProcedureService;
import mesweb.mesweb.vo.procecdure.MesProduceProcedureBranchProcedureVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Collection;
import java.util.List;

 /**
 * @Description: 生产支线工序关系
 * @Author: jeecg-boot
 * @Date:   2021-06-26
 * @Version: V1.0
 */

@Api(tags="生产支线工序关系")
@RestController
@RequestMapping("/mes/mesProduceProcedureBranchProcedure")
public class MesProduceProcedureBranchProcedureController {
	@Autowired
	private IMesProduceProcedureBranchProcedureService mesProduceProcedureBranchProcedureService;


    @GetMapping("/GetAll")
    @ApiOperation("获取全部数据列表")
    public CommonResult<?> GetAll(@ApiParam("名称") @RequestParam( value = "name",required = false) String name) {
        List<MesProduceProcedureBranchProcedureVo> data;
		QueryWrapper<MesProduceProcedureBranchProcedureVo> queryWrapper = new QueryWrapper<>();
		if (StringUtils.isBlank(name) || name.equals("全部")) {

        } else {
            }
		data = mesProduceProcedureBranchProcedureService.getProduceProcedureBranchProcedure(queryWrapper);
		return CommonResult.success(data);
    }
	/**
	 * 分页列表查询
	 *
	 * @param mesProduceProcedureBranchProcedure
	 * @param page
	 * @param page
	 * @param req
	 * @return
	 */
	@ApiOperation(value="生产支线工序关系-分页列表查询", notes="生产支线工序关系-分页列表查询")
	@GetMapping(value = "/list")
	public CommonResult<?> queryPageList(MesProduceProcedureBranchProcedure mesProduceProcedureBranchProcedure,
								   @RequestParam(name="page", defaultValue="1") Integer page,
								   @RequestParam(name="size", defaultValue="10") Integer size,
								   HttpServletRequest req) {
		QueryWrapper<MesProduceProcedureBranchProcedureVo> queryWrapper = new QueryWrapper<>();
		Page<MesProduceProcedureBranchProcedureVo> pageMesProduceProcedureBranchProcedure = new Page<>(page, size);
		IPage<MesProduceProcedureBranchProcedureVo> pageList = mesProduceProcedureBranchProcedureService.getProduceProcedureBranchProcedure(pageMesProduceProcedureBranchProcedure, queryWrapper);
		return CommonResult.success(pageList);
	}
	/**
	 * 编辑
	 *
	 * @param mesProduceProcedureBranchProcedure
	 * @return
	 */
	@ApiOperation(value="生产支线工序关系-编辑", notes="生产支线工序关系-编辑")
	@PostMapping(value = "/save")
	public CommonResult<?> edit(@RequestBody MesProduceProcedureBranchProcedure mesProduceProcedureBranchProcedure) {
       boolean result = false;
        if(mesProduceProcedureBranchProcedure.getId() == 0L) {
             result = mesProduceProcedureBranchProcedureService.save(mesProduceProcedureBranchProcedure);
        }
        else
        {
            result = mesProduceProcedureBranchProcedureService.updateById(mesProduceProcedureBranchProcedure);
        }
        return CommonResult.success(mesProduceProcedureBranchProcedure);
	}
	 /**
	  * 编辑
	  *
	  * @param mesProduceProcedureBranchProcedures
	  * @return
	  */
	 @ApiOperation(value="生产工序支线和工序关系-批量增加", notes="生产工序支线和工序关系-批量增加")
	 @PostMapping(value = "/saveBatch")
	 public CommonResult<?> edit(@RequestBody Collection<MesProduceProcedureBranchProcedure> mesProduceProcedureBranchProcedures) {
		 boolean result = false;
		 try {
			 if (mesProduceProcedureBranchProcedures.size() > 0L) {
				 for (MesProduceProcedureBranchProcedure mesProduceProcedureBranchProcedure :
						 mesProduceProcedureBranchProcedures) {
					 result = mesProduceProcedureBranchProcedureService.save(mesProduceProcedureBranchProcedure);
				 }
			 }
			 return CommonResult.success(mesProduceProcedureBranchProcedures);
		 }catch (Exception e)
		 {
			 return  CommonResult.failed(e.getMessage());
		 }
	 }

	/**
	 * 通过id删除
	 *
	 * @param id
	 * @return
	 */
	@ApiOperation(value="生产支线工序关系-通过id删除", notes="生产支线工序关系-通过id删除")
	@DeleteMapping(value = "/delete")
	public CommonResult<?> delete(@RequestParam(name="id",required=true) String id) {
		mesProduceProcedureBranchProcedureService.removeById(id);
		return CommonResult.success("删除成功!");
	}

	/**
	 * 批量删除
	 *
	 * @param ids
	 * @return
	 */
	@ApiOperation(value="生产支线工序关系-批量删除", notes="生产支线工序关系-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public CommonResult<?> deleteBatch(@ApiParam("MesProduceProcedureBranchProcedureIDs") @RequestBody List<Long> ids) {
		this.mesProduceProcedureBranchProcedureService.removeByIds(ids);
		return CommonResult.success("批量删除成功！");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@ApiOperation(value="生产支线工序关系-通过id查询", notes="生产支线工序关系-通过id查询")
	@GetMapping(value = "/queryById")
	public CommonResult<?> queryById(@RequestParam(name="id",required=true) String id) {
		MesProduceProcedureBranchProcedure mesProduceProcedureBranchProcedure = mesProduceProcedureBranchProcedureService.getById(id);
		return CommonResult.success(mesProduceProcedureBranchProcedure);
	}
}
